package com.example.backend.controller;


import com.example.backend.common.ResultMessage;
import com.example.backend.entity.ShenFen;
import com.example.backend.service.IShenFenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/shenfen")
@CrossOrigin
public class ShenFenController {
    @Autowired
    private IShenFenService shenFenService;

    @PostMapping
    public ResultMessage addShenFen(@RequestBody ShenFen shenFen) {
        if(shenFenService.addShenFen(shenFen)) {
            Date date = new Date();
            shenFen.setZcsj(String.valueOf(date));
            return ResultMessage.success(shenFen,"新增成功");
        }
        return ResultMessage.error("新增失败");
    }

    @PostMapping("/login")
    public ResultMessage loginShenFen(@RequestBody ShenFen shenFen) {
        ShenFen shenFen1 = shenFenService.queryShenFenByYhzh(shenFen);
        System.out.println(shenFen1);
        if(shenFen1 == null) {
            return ResultMessage.error("账号或密码错误");
        }
        return ResultMessage.success(shenFen1,"登录成功");
    }

    @DeleteMapping("/{yhid}")
    public ResultMessage deleteShenFen(@PathVariable Integer yhid) {
        if(shenFenService.deleteShenFen(yhid)) {
            return ResultMessage.success(null,"删除成功");
        }
        return ResultMessage.error("删除失败");
    }

    @PutMapping
    public ResultMessage updateShenFen(@RequestBody ShenFen shenFen) {
        if(shenFenService.updateShenFen(shenFen)) {
            return ResultMessage.success(shenFen,"更新成功");
        }
        return ResultMessage.error("更新失败");
    }

    @PostMapping("/forgot")
    public ResultMessage forgotPwd(@RequestBody ShenFen shenFen) {
        if(shenFenService.forgotPwd(shenFen)) {
            return ResultMessage.success(null,"重置成功");
        }
        return ResultMessage.error("重置失败");
    }


    @GetMapping
    public ResultMessage queryShenFen() {
        return ResultMessage.success(shenFenService.queryShenFen(),"查询成功");
    }

    @GetMapping("/{yhid}")
    public ResultMessage queryShenFenById(@PathVariable Integer yhid) {
        return ResultMessage.success(shenFenService.queryShenFenById(yhid),"查询成功");
    }
}
